package com.qq.BFMRSE.requestModule;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import com.qq.BFMRSE.util.BFConstants;
public class downFilesModule {
	public static int downLoadFile(String url,List<String>fileNames,String userName,String fileDir){
		HttpClient httpClient=new HttpClient();
		PostMethod post=new PostMethod(url);
		StringBuffer sb=new StringBuffer();
		int len=fileNames.size();
		for(int i=0;i<len;i++){
			sb.append(fileNames.get(i));
			if(i!=len-1){
				sb.append(",");
			}
		}
		post.addParameter("userName", userName);
		post.addParameter("downLoads",sb.toString());
		InputStream in;
		int stateCode=-1;
		try {
			stateCode=httpClient.executeMethod(post);
			in = post.getResponseBodyAsStream();
			BufferedInputStream bin=new BufferedInputStream(in);
			String desFileName=fileDir;
			if(len==1){
				desFileName=desFileName+File.separator+fileNames.get(0);
			}else {
				desFileName=desFileName+File.separator+BFConstants.DOWNZIPFILENAME;
			}
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(desFileName)));
			int length=0;
			byte []buffer=new byte[2048];
			while((length=bin.read(buffer))>0){
				bos.write(buffer,0,length);
			}
			bos.close();
			bin.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stateCode;
	}
}
